/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.uniceub.fsw.agenda.model.facade;

import br.uniceub.fsw.agenda.exception.BusinessException;
import br.uniceub.fsw.agenda.exception.DatabaseException;
import br.uniceub.fsw.agenda.model.dao.MenuDAO;
import br.uniceub.fsw.agenda.model.vo.MenuVO;
import br.uniceub.fsw.agenda.model.dao.UFDAO;
import br.uniceub.fsw.agenda.model.vo.UFVO;
import br.uniceub.fsw.agenda.util.StringUtils;
import java.util.List;

/**
 *
 * @author Bruno
 */
public class ManterUFFacade extends GenericFacade {

    /**
     * Busca a UF pela sigla
     * @param sgUf sigla da UF
     * @return objeto UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public UFVO findUFBySigla(String sgUf) throws DatabaseException, BusinessException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            return dao.findBySigla(sgUf);
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Busca lista de UF pela sigla
     * @param sgUf sigla da UF
     * @return lista de objeto UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public List<UFVO> findAllUFsBySigla(String sgUf) throws DatabaseException, BusinessException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            return dao.findAllBySigla(sgUf);
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Busca todas as UFs
     * @return List com objetos UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public List<MenuVO> findAllMenu() throws DatabaseException, BusinessException {
        try {
            MenuDAO dao = new MenuDAO(getConnection());
            return dao.findAll();
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Busca todas as UFs ou somente aquelas que possuem parte da sigla
     * ou toda a sigla passada
     * @return List com objetos UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public List<UFVO> findAllUFs(String sgUf, String noUf) throws DatabaseException, BusinessException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            
            if(StringUtils.isNullOrEmpty(sgUf) && StringUtils.isNullOrEmpty(noUf)) {
                dao.findAll();
            } else if(StringUtils.isNullOrEmpty(sgUf) && StringUtils.isNullOrEmpty(noUf)) {
                dao.findAllByNome(noUf);
            } else {
                dao.findAllBySigla(sgUf);
            }
            
            return dao.findAll();
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Busca todas as UFs
     * @return List com objetos UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public List<UFVO> findAllUFs() throws DatabaseException, BusinessException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            return dao.findAll();
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Busca todas as UFs por parte do nome
     * @param noUf nome do UF
     * @return List com objetos UF
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     * @throws br.uniceub.fsw.agenda.exception.BusinessException
     */
    public List<UFVO> findAllUFsByNome(String noUf) throws DatabaseException, BusinessException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            return dao.findAllByNome(noUf);
        } finally {
            closeConnection();
        }
    }
    
    /**
     * Insere um novo UF.
     * @param UF
     * @return ID auto-increment gerada pra esse registro
     * @throws br.uniceub.fsw.agenda.exception.BusinessException 
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     */
    public Integer inserirUF(UFVO uf) throws BusinessException, DatabaseException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            
            String sgUf = uf.getSgUf();
            String noUf = uf.getNoUf();

            //tratamento de erros
            if(StringUtils.isNullOrEmpty(sgUf)) {
                throw new BusinessException("Sigla do UF é obrigatória.");
            }
            if(StringUtils.isNullOrEmpty(noUf)) {
                throw new BusinessException("Nome do UF é obrigatório.");
            }
        
            return dao.insert(uf);
        } finally {
            closeConnection();
        }        
    }
    
    /**
     * Atualiza um UF.
     * @param UF
     * @throws br.uniceub.fsw.agenda.exception.BusinessException 
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     */
    public void alterarUF(UFVO uf) throws BusinessException, DatabaseException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            
            String sgUf = uf.getSgUf();
            String noUf = uf.getNoUf();

            //tratamento de erros
            if(StringUtils.isNullOrEmpty(sgUf)) {
                throw new BusinessException("Sigla do UF é obrigatória.");
            }
            if(StringUtils.isNullOrEmpty(noUf)) {
                throw new BusinessException("Nome do UF é obrigatório.");
            }
        
            dao.update(uf);
        } finally {
            closeConnection();
        }        
    }
    
    /**
     * Exclui um UF.
     * @param UF
     * @throws br.uniceub.fsw.agenda.exception.BusinessException 
     * @throws br.uniceub.fsw.agenda.exception.DatabaseException
     */
    public void excluirUF(UFVO UF) throws BusinessException, DatabaseException {
        try {
            UFDAO dao = new UFDAO(getConnection());
            dao.delete(UF);
        } finally {
            closeConnection();
        }        
    }
    
}
